﻿using QuanLyGiaoVuService.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace QuanLyGiaoVuService.CommonServices
{
    public class TemplateService : ITemplateService
    {
        private IThamSoService _thamSoService;

        public TemplateService(IThamSoService thamSoService)
        {
            _thamSoService = thamSoService;
        }

        public IEnumerable<TemplateXMLModel> GetAllTemplateXMLElements(string templateName)
        {
            List<TemplateXMLModel> tokenList = new List<TemplateXMLModel>();
            string xmlContent = _thamSoService.GetThamSo(templateName);
            XmlDocument document = new XmlDocument();
            document.LoadXml(xmlContent);
            var rootNode = document.FirstChild;
            if (rootNode != null)
            {
                XmlNodeList childNodeList = rootNode.ChildNodes;
                foreach (XmlNode node in childNodeList)
                {
                    TemplateXMLModel token = new TemplateXMLModel();
                    token.PropertyName = node.Attributes["Name"] != null ? node.Attributes["Name"].Value : string.Empty;
                    token.TokenValue = node.InnerText;
                    token.PropertyCode = node.Name;
                    token.Description = node.Attributes["Description"] != null ? node.Attributes["Description"].Value : string.Empty;
                    tokenList.Add(token);
                }
            }
            return tokenList;
        }

        public Domain.TemplateXMLModel UpdateTemplateXMLElement(Domain.TemplateXMLModel templateXML)
        {
            throw new NotImplementedException();
        }
    }
}
